<template>
  <!-- 详情 -->
  <el-dialog
    title="新增角色"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto">
      <el-form-item label="角色编码" prop="code">
        <el-input
          v-model="ruleForm.code"
          placeholder="请输入角色编码"
        ></el-input>
      </el-form-item>
      <el-form-item label="角色名称" prop="name">
        <el-input
          v-model="ruleForm.name"
          placeholder="请输入角色名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        type: 0,
      },
      rules: {
        code: [{ required: true, message: "请输入角色编码", trigger: "blur" }],
        name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$confirm("是否确认此操作?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            this.$post({
              url: "/role",
              params: this.ruleForm,
            }).then((res) => {
              this.$message.success("操作成功");
              this.handleClose();
              this.$parent.$parent.type = 0;
              this.$parent.$parent.page.current = 1;
              this.$parent.$parent.page.size = 10;

              this.$parent.$parent.getList();
            });
          });
        }
      });
    },
    handleOpen() {
      this.dialogVisible = true;
    },
    handleClose() {
      this.dialogVisible = false;
      this.ruleForm = {
        type: 0,
      };
    },
  },
};
</script>
